<template>
  <el-dialog title="关联角色" :visible.sync="showDialog" width="40%" @close="">
    <!-- 内容 -->
    <!-- 多选组 label的内容最终会给v-model绑定的变量 -->
    <!-- 展示的内容也可以是插槽里面的信息 -->
    <el-checkbox-group v-model="checkRoleList">
      <el-checkbox
        v-for="item in allRoleList"
        :key="item.id"
        :label="item.id"
        >{{ item.name }}</el-checkbox
      >
    </el-checkbox-group>
    <span slot="footer">
      <el-button @click="showDialog = false">取消</el-button>
      <el-button type="primary" @click="btnOK">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getRoleListApi } from "@/api/setting";
import { setRoleAuth } from "@/api/employees";
export default {
  name: "setRole",
  data() {
    return {
      showDialog: false,
      allRoleList: [], // 所有角色
      checkRoleList: [], // 选中的角色
      userId: null, // 员工ID
    };
  },
  methods: {
    // 获取所有角色
    async getRoleList() {
      let { rows } = await getRoleListApi({ page: 1, pagesize: 1000 });
      this.allRoleList = rows;
    },
    closeDialog() {
      this.checkRoleList = [];
      this.userId = null;
    },
    async btnOK() {
      await setRoleAuth({
        id: this.userId,
        roleIds: this.checkRoleList,
      });
      this.$message.success("关联角色成功");
      this.showDialog = false;
    },
  },
};
</script>
<style scoped></style>
